package my.xh.validate.handler.impl;

import my.xh.validate.ValidateField;
import my.xh.validate.handler.ValidateHandler;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName DateRuleValidateHandler
 * @Date 2019/1/17 15:43
 * @Author xuanhu
 * @Description TODO
 */
@Component
public class DateRuleValidateHandler implements ValidateHandler {
    private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public boolean validate(ValidateField validateField, StringBuilder paramName, Object paramValue, Map<String, Object> map, boolean flag) {
        if (!"".equals(validateField.dateRule().trim())) {
            try {
                String[] rule = validateField.dateRule().split(",");

                Date date = (paramValue instanceof Date) ? (Date) paramValue : df.parse(paramValue.toString());
                Date target = null;
                if ("now".equals(rule[1].trim().toLowerCase())) target = new Date();
                else target = df.parse(rule[1]);

                if (rule.length == 4) {
                    Calendar instance2 = Calendar.getInstance();
                    instance2.setTimeInMillis(target.getTime());
                    int type = (int) Calendar.class.getField(rule[3]).get(instance2);
                    if ("<".equals(rule[0])) {
                        instance2.add(type, -1 * Integer.valueOf(rule[2]));
                        if (date.getTime() > instance2.getTimeInMillis()) {
                            flag = false;
                            map.put(paramName.toString(), getMsg(validateField, paramName.append(" 的值不能大于 ").append(rule[1]).append(" ").append(rule[2]).append(" ").append(rule[3]).toString()));
                        }
                    } else if (">".equals(rule[0])) {
                        instance2.add(type, Integer.valueOf(rule[2]));
                        if (date.getTime() < instance2.getTimeInMillis()) {
                            flag = false;
                            map.put(paramName.toString(), getMsg(validateField, paramName.append(" 的值不能小于 ").append(rule[1]).append(" ").append(rule[2]).append(" ").append(rule[3]).toString()));
                        }
                    }

                } else if (rule.length == 2) {
                    if ("<".equals(rule[0])) {
                        if (date.getTime() > target.getTime()) {
                            flag = false;
                            map.put(paramName.toString(), getMsg(validateField, paramName.append(" 的值不能大于 ").append(rule[1]).append(" ").toString()));
                        }
                    } else if (">".equals(rule[0])) {
                        if (date.getTime() < target.getTime()) {
                            flag = false;
                            map.put(paramName.toString(), getMsg(validateField, paramName.append(" 的值不能小于 ").append(rule[1]).append(" ").toString()));
                        }
                    }
                } else {
                    throw new RuntimeException("dateRule配置不正确");
                }
            } catch (Exception e) {
                if (e.getMessage().equals("dateRule配置不正确")) throw (RuntimeException) e;
                flag = false;
                map.put(paramName.toString(), getMsg(validateField, paramName.append(" 的值不匹配").toString()));
            }

        }

        return flag;
    }
}
